#include "fig_ui_settings.h"
#include "gen_game_state.h"
#include "build.h"

#include "ui/ui_manager.h"
#include "gameflow/game_flow_manager.h"
#include "gameflow/ui_popup.h"
#include "gameflow/ui_state.h"
#include "application/game_application.h"
#include "application/game_rms.h"





FIGUISettings::FIGUISettings(gameflow::UIPopup* pPopup) : 
									FIGUIPopupAnimator(pPopup),
									m_IsShowingAnimation(false),
									m_AnimationAlpha(1.0f)
{

	
	if(m_pPopup)
	{
		SetCloseButton("Resume_2");

		AddToNaviationUI(m_pPopup->GetWidget("Resume_2"));

#ifndef _NO_CREDITBUTTON
		AddToNaviationUI(m_pPopup->GetWidget("Credits"));
#else
		if(m_pPopup->GetWidget("Credits"))
		{
			m_pPopup->GetWidget("Credits")->SetIsVisible(false);
		}
#endif

		//m_pAnimateWidget = m_pPopup->GetWidget("SettingBG");
		FIGUIPopupAnimatorMgr::Instance().AddPopupAnimator(FIGUIPopupAnimatorMgr::POPUP_NAMES_SETTING,this);
		
		m_Type = ANIMATION_SLIDEFROMRIGHT;

		m_pPopup->SetWidgetFunctor("Resume_2",UI_DELEGATE(FIGUISettings, OnButtonResume));
		
#ifdef _PLATFORM_IOS
        m_pPopup->SetWidgetFunctor("Resume",UI_DELEGATE(FIGUISettings, OnButtonResume));
#endif
        
	}
}

FIGUISettings::~FIGUISettings()
{

}

void					FIGUISettings::Show()
{
	
	if(m_pPopup)
	{
		if(m_pPopup->GetWidget("TiltOn"))
			m_pPopup->GetWidget("TiltOn")->SetIsVisible(!input::MOGAJoystick::Instance().IsConnected());

	}

	m_IsShowingAnimation = true;
	m_AnimationAlpha = 0;
	
	FIGUIPopupAnimator::Show();
}

void					FIGUISettings::Hide()
{
	if(g_pApp && g_pApp->GetRMS())
        g_pApp->GetRMS()->Save();

	GENGameState::GetGame()->UnPause();

	if(FIGUIPopupAnimatorMgr::Instance().GetPopup(FIGUIPopupAnimatorMgr::POPUP_NAMES_PAUSEMENU))
		FIGUIPopupAnimatorMgr::Instance().GetPopup(FIGUIPopupAnimatorMgr::POPUP_NAMES_PAUSEMENU)->Hide();

	FIGUIPopupAnimator::Hide();
}

void DELEGATE_CALL		FIGUISettings::OnButtonResume(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
    
	Hide();
	

	//MATUIAnimator::Instance().Add(m_pAnimateWidget,MATAnimatedUI::ANIMATE_STYLE_HIDE_TORIGHT,0, this);
}

void			FIGUISettings::OnAnimatedUIFinish()
{
	if(m_pPopup)
		m_pPopup->Hide(NULL);
}

void	FIGUISettings::Tick(float dt)
{
	FIGUIPopupAnimator::Tick(dt);
	if(!m_pPopup->IsActive())
		return;

	if(!m_IsShowingAnimation)
		return;

	m_AnimationAlpha += ( 2.0f * dt);
	if(m_AnimationAlpha >= 1.0f)
	{
		m_AnimationAlpha = 1.0f;
		m_IsShowingAnimation = false;
	}

	m_pPopup->GetWidget("Resume_2")->SetColor(1.0f,1.0f,1.0f,m_AnimationAlpha);
	m_pPopup->GetWidget("TiltOn")->SetColor(1.0f,1.0f,1.0f,m_AnimationAlpha);
	m_pPopup->GetWidget("Music_volume1")->SetColor(1.0f,1.0f,1.0f,m_AnimationAlpha);
	m_pPopup->GetWidget("Sound_volume1")->SetColor(1.0f,1.0f,1.0f,m_AnimationAlpha);	

}

bool	FIGUISettings::KeyPressed(input::Keyboard::KEY* pKey)
{
	if(m_pPopup->IsActive() && pKey->KeyID == KEY_ESCAPE)
	{
		Hide();
		//MATUIAnimator::Instance().Add(m_pAnimateWidget,MATAnimatedUI::ANIMATE_STYLE_HIDE_TORIGHT,0, this);
		return true;
	}
	return false;
}


